﻿using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace Purchase.Domain
{
    /// <summary>
    /// 物料清单主表实体
    /// </summary>
    public class BillOfMaterial
    {
        /// <summary>
        /// 物料清单ID，主键，自增长
        /// </summary>
        [Key]
        public int BomId { get; set; }

        /// <summary>
        /// 物料清单编号，必填，最大长度50
        /// </summary>
        [Required(ErrorMessage = "物料清单编号不能为空")]
        [StringLength(50, ErrorMessage = "物料清单编号长度不能超过50个字符")]
        [Display(Name = "物料清单编号")]
        public string BomNumber { get; set; }

        /// <summary>
        /// 物料清单主题，最大长度100
        /// </summary>
        [StringLength(100, ErrorMessage = "物料清单主题长度不能超过100个字符")]
        [Display(Name = "物料清单主题")]
        public string BomSubject { get; set; }

        /// <summary>
        /// 产品名称，必填，最大长度100
        /// </summary>
        [Required(ErrorMessage = "产品名称不能为空")]
        [StringLength(100, ErrorMessage = "产品名称长度不能超过100个字符")]
        [Display(Name = "产品名称")]
        public string ProductName { get; set; }

        /// <summary>
        /// 产品编码，最大长度50
        /// </summary>
        [StringLength(50, ErrorMessage = "产品编码长度不能超过50个字符")]
        [Display(Name = "产品编码")]
        public string ProductCode { get; set; }

        /// <summary>
        /// 规格型号，最大长度100
        /// </summary>
        [StringLength(100, ErrorMessage = "规格型号长度不能超过100个字符")]
        [Display(Name = "规格型号")]
        public string Specification { get; set; }

        /// <summary>
        /// 单位，最大长度20
        /// </summary>
        [StringLength(20, ErrorMessage = "单位长度不能超过20个字符")]
        [Display(Name = "单位")]
        public string Unit { get; set; }

        /// <summary>
        /// 数量，默认为1，精度18位，小数2位
        /// </summary>
        [Column(TypeName = "decimal(18,2)")]
        [Display(Name = "数量")]
        public decimal Quantity { get; set; } = 1;

        /// <summary>
        /// 产品角色，最大长度20，默认为"自制"
        /// </summary>
        [StringLength(20, ErrorMessage = "产品角色长度不能超过20个字符")]
        [Display(Name = "产品角色")]
        public string ProductRole { get; set; } = "自制";

        /// <summary>
        /// 工艺流程，最大长度100
        /// </summary>
        [StringLength(100, ErrorMessage = "工艺流程长度不能超过100个字符")]
        [Display(Name = "工艺流程")]
        public string ProcessFlow { get; set; }

        /// <summary>
        /// 耗损率(%)，精度5位，小数2位
        /// </summary>
        [Column(TypeName = "decimal(5,2)")]
        [Display(Name = "耗损率(%)")]
        public decimal? LossRate { get; set; }

        /// <summary>
        /// 制单人员，必填，最大长度50
        /// </summary>
        [Required(ErrorMessage = "制单人员不能为空")]
        [StringLength(50, ErrorMessage = "制单人员长度不能超过50个字符")]
        [Display(Name = "制单人员")]
        public string CreatedBy { get; set; }

        /// <summary>
        /// 创建日期，默认为当前时间
        /// </summary>
        [Display(Name = "创建日期")]
        public DateTime CreatedDate { get; set; } = DateTime.Now;

        /// <summary>
        /// 备注，最大长度500
        /// </summary>
        [StringLength(500, ErrorMessage = "备注长度不能超过500个字符")]
        [Display(Name = "备注")]
        public string Remarks { get; set; }

        /// <summary>
        /// 子件列表，一对多关系
        /// </summary>
        [JsonIgnore] //在实体类中标记不需要序列化的属性：
        [Display(Name = "子件列表")]
        public List<BomComponent> Components { get; set; } = new List<BomComponent>();
    }


    /// <summary>
    /// 物料清单子件表实体
    /// </summary>
    public class BomComponent
    {
        /// <summary>
        /// 子件ID，主键，自增长
        /// </summary>
        [Key]
        public int ComponentId { get; set; }

        /// <summary>
        /// 关联的物料清单ID，外键
        /// </summary>
        [Display(Name = "物料清单ID")]
        public int BomId { get; set; }

        /// <summary>
        /// 序号，必填
        /// </summary>
        [Required(ErrorMessage = "序号不能为空")]
        [Display(Name = "序号")]
        public int SequenceNumber { get; set; }

        /// <summary>
        /// 产品名称，必填，最大长度100
        /// </summary>
        [Required(ErrorMessage = "产品名称不能为空")]
        [StringLength(100, ErrorMessage = "产品名称长度不能超过100个字符")]
        [Display(Name = "产品名称")]
        public string ProductName { get; set; }

        /// <summary>
        /// 产品编号，最大长度50
        /// </summary>
        [StringLength(50, ErrorMessage = "产品编号长度不能超过50个字符")]
        [Display(Name = "产品编号")]
        public string ProductCode { get; set; }

        /// <summary>
        /// 规格型号，最大长度100
        /// </summary>
        [StringLength(100, ErrorMessage = "规格型号长度不能超过100个字符")]
        [Display(Name = "规格型号")]
        public string Specification { get; set; }

        /// <summary>
        /// 单位，最大长度20
        /// </summary>
        [StringLength(20, ErrorMessage = "单位长度不能超过20个字符")]
        [Display(Name = "单位")]
        public string Unit { get; set; }

        /// <summary>
        /// 产品比例，精度18位，小数4位
        /// </summary>
        [Column(TypeName = "decimal(18,4)")]
        [Display(Name = "产品比例")]
        public decimal? ProductRatio { get; set; }

        /// <summary>
        /// 数量，必填，精度18位，小数2位
        /// </summary>
        [Required(ErrorMessage = "数量不能为空")]
        [Column(TypeName = "decimal(18,2)")]
        [Display(Name = "数量")]
        public decimal Quantity { get; set; }

        /// <summary>
        /// 产品角色，最大长度20
        /// </summary>
        [StringLength(20, ErrorMessage = "产品角色长度不能超过20个字符")]
        [Display(Name = "产品角色")]
        public string ProductRole { get; set; }

        /// <summary>
        /// 工艺流程，最大长度100
        /// </summary>
        [StringLength(100, ErrorMessage = "工艺流程长度不能超过100个字符")]
        [Display(Name = "工艺流程")]
        public string ProcessFlow { get; set; }

        /// <summary>
        /// 耗损率(%)，精度5位，小数2位
        /// </summary>
        [Column(TypeName = "decimal(5,2)")]
        [Display(Name = "耗损率(%)")]
        public decimal? LossRate { get; set; }

        /// <summary>
        /// 领料方式，最大长度50
        /// </summary>
        [StringLength(50, ErrorMessage = "领料方式长度不能超过50个字符")]
        [Display(Name = "领料方式")]
        public string PickingMethod { get; set; }

        /// <summary>
        /// 备注，最大长度500
        /// </summary>
        [StringLength(500, ErrorMessage = "备注长度不能超过500个字符")]
        [Display(Name = "备注")]
        public string Remarks { get; set; }

        /// <summary>
        /// 导航属性 - 关联的物料清单主表
        /// </summary>
        [ForeignKey("BomId")]
        [Display(Name = "关联物料清单")]
        public BillOfMaterial BillOfMaterial { get; set; }
    }
}
